package com.longy.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longy.gulimall.product.entity.ProductAttrValueEntity;
import com.longy.gulimall.product.service.ProductAttrValueService;
import com.longy.gulimall.product.vo.AttrGroupRelationVo;
import com.longy.gulimall.product.vo.AttrRespVo;
import com.longy.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import com.longy.gulimall.product.entity.AttrEntity;
import com.longy.gulimall.product.service.AttrService;
import com.longy.common.utils.PageUtils;
import com.longy.common.utils.R;



/**
 * 商品属性
 *
 * @author longyang
 * @email longyang3240@163.com
 * @date 2020-11-14 16:27:07
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    ///base/list/0?t=1608458332219&page=1&limit=10&key=
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryBaseAttrList(params,catelogId,attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/hello")
    public String list(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("hah","123"+ UUID.randomUUID());
        String hah = ops.get("hah");
        return  hah;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    /**
     * 获取spu规格
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R delete(@PathVariable Long spuId){
        List<ProductAttrValueEntity> attr_id = productAttrValueService.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        return R.ok().data(attr_id);
    }

}
